package at.ac.tuwien.infosys.aic11.legacy;

import java.util.ArrayList;
import java.util.HashMap;

import at.ac.tuwien.infosys.aic11.dto.CreditRequest;
import at.ac.tuwien.infosys.aic11.dto.Duration;
import at.ac.tuwien.infosys.aic11.dto.InterestRate;
import at.ac.tuwien.infosys.aic11.dto.Money;
import at.ac.tuwien.infosys.aic11.dto.Offer;
import at.ac.tuwien.infosys.aic11.dto.Rating;


public class ContractManagementMock {

	private long id = 100;
		
	//singleton
	private static ContractManagementMock instance=null;
	private ContractManagementMock(){}
	public static ContractManagementMock getInstance(){
		if(instance==null)
			instance = new ContractManagementMock();
		return instance;
	}
	
	private HashMap<Long,Offer> openRequests = new HashMap<Long,Offer>();
	
	public Offer generateOffer(CreditRequest cr){
		if(openRequests.containsKey(new Long(cr.getRequestId()))){
			return openRequests.get(new Long(cr.getRequestId()));
		}
		
		Rating r = cr.getCustomer().getRating();
		Money m = cr.getMoney();
		Duration d = cr.getDuration();
		
		id++;
		Offer o = new Offer();
		o.setOfferId(id);
		o.setComments("das ist ein kommentar. "+id);
		
		InterestRate ir = new InterestRate();
		String rating = r.getRating();
		
		if(rating.equals("AAA")||rating.equals("AAPlus")){
			ir.setRate(3);
		} else{
			ir.setRate(10);
		}
		
		if(m.getAmount()>10000){
			ir.setRate(ir.getRate()+0.5);
		}
		
		ir.setRate(ir.getRate()+(d.getYears()/10));
		o.setInterestRate(ir);
		openRequests.put(new Long(cr.getRequestId()),o);		
		return o;
	}
	
	public void deleteRequest(CreditRequest cr){
		openRequests.remove(new Long(cr.getRequestId()));
	}
}
